<template>
  <div>
    <el-form :model="formItem" label-width="120px" size="mini" class="form-view">
      <el-row :gutter="40">
        <el-col :span="24">
          <el-form-item label="公司名称：">
            {{ formItem.name }}
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="驻场人员：">
            {{ getName(formItem.residents) }}
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="变更历史">
            <el-table
              :data="logs"
              :border="false"
              size="mini"
              style="width: 100%"
            >
              <el-table-column v-for="column in columns" :key="column.key" :label="column.title" :prop="column.key">
                <template slot-scope="scope">
                  <column-render :render="column.render" :row="scope.row" :column="column" />
                </template>
              </el-table-column>
            </el-table>
          </el-form-item>

        </el-col>
      </el-row>

    </el-form>
  </div>
</template>

<script>
import { detail, logs } from '@neu/dcom-ui/api/institutions'
// 定义一个表单原始项
// 定义一个表单原始项
const formItem = {
  // 公司名称
  name: '',
  // 公司编码
  code: '',
  residents: [],
  users: []
}

export default {
  name: 'InstitutionsViewForm',
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      // 查看的表单内容
      formItem: {
        ...formItem
      },
      logs: [],
      columns: [
        { title: '变更时间', key: 'changeAt' },
        { title: '原驻场工程师', key: 'residents', render: (h, params) => { return h('span', this.getName(params.row.residents)) } }
      ]
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: function() {
      this.fetch()
    }
  },
  created() {
    this.fetch()
  },
  methods: {
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        detail(this.id).then(response => {
          this.formItem = {
            ...response.data
          }
        })
        logs(this.id).then(response => {
          this.logs = response.data
          console.log(response)
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    getName(user) {
      return user.map(e => e.name + '  ').join(' ')
    }
  }
}
</script>
